package com.zy.yygh.hosp.controller.api;

import com.zy.yygh.common.exception.YyghException;
import com.zy.yygh.common.helper.HttpRequestHelper;
import com.zy.yygh.common.result.Result;
import com.zy.yygh.common.result.ResultCodeEnum;
import com.zy.yygh.common.utils.MD5;
import com.zy.yygh.hosp.service.DepartmentService;
import com.zy.yygh.hosp.service.HospitalService;
import com.zy.yygh.hosp.service.HospitalSetService;
import com.zy.yygh.hosp.service.ScheduleService;
import com.zy.yygh.model.hosp.Department;
import com.zy.yygh.model.hosp.Hospital;
import com.zy.yygh.model.hosp.Schedule;
import com.zy.yygh.vo.hosp.DepartmentQueryVo;
import com.zy.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

//    删除排班接口
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){

//        传递过来的排班信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
//        医院编号 排班
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

//        TODO 签名验证
        //        医院签名key和数据库key
        String signKey = (String)paramMap.get("sign");
        String HospSign = hospitalSetService.getSignKey(hoscode);
//        数据库查出以后加密处理
        String HospSignMd5 = MD5.encrypt(HospSign);
        if (!HospSignMd5.equals(signKey)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode,hosScheduleId);

        return Result.ok();
    }

//    查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
//        传递过来的排班信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
//        医院编号 科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

//        当前页和每页记录数
        String setPage = (String)paramMap.get("page");
        String setLimit =(String)paramMap.get("limit");

        int page = StringUtils.isEmpty(setPage)?1:Integer.parseInt(setPage);
        int limit = StringUtils.isEmpty(setLimit)?1:Integer.parseInt(setLimit);

//        医院签名key和数据库key
        String signKey = (String)paramMap.get("sign");
        String HospSign = hospitalSetService.getSignKey(hoscode);
//        数据库查出以后加密处理
        String HospSignMd5 = MD5.encrypt(HospSign);
        if (!HospSignMd5.equals(signKey)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

//        调用service方法
       Page<Schedule>schedules= scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(schedules);
    }


//    上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
//        获取参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
//        获取医院签名和编号
        String hoscode = (String)paramMap.get("hoscode");
        String hosSign = (String)paramMap.get("sign");

//        TODO 签名校验
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);

        if (!signKeyMd5.equals(hosSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);

        return Result.ok();
    }

//    删除科室信息接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
//        获取医院编码 科室编码 唯一标识
        String hoscode = (String)paramMap.get("hoscode");
        String hospSign = (String)paramMap.get("sign");
        String depcode = (String)paramMap.get("depcode");

//        TODO 签名验证
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }



//    查询科室信息
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
//        获取传过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

//        获取医院编号和当前页和一页最多的值
        String hoscode =(String)paramMap.get("hoscode");
        String hospSign = (String)paramMap.get("sign");
        String stringPage = (String) paramMap.get("page");
        String newLimit = (String) paramMap.get("limit");


        int page = StringUtils.isEmpty(stringPage)
                ? 1 : Integer.parseInt(stringPage);

        int limit = StringUtils.isEmpty(newLimit)
                ? 1 : Integer.parseInt(newLimit);

//        TODO 签名的校验
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
//        调用service中的方法
        Page<Department> pageModel=departmentService.findPageDepartment(page,limit,departmentQueryVo);

        return Result.ok(pageModel);
    }



//    上传科室系统
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
//        获取传过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
//        获取医院编号
        String hoscode = (String)paramMap.get("hoscode");
//        获取签名sign
        String hospSign = (String)paramMap.get("sign");
//        根据编号查询科室信息签名
        String signKey = hospitalSetService.getSignKey(hoscode);
//        把数据库签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
//        判断签名是否一致
        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
//        调用service方法
        departmentService.save(paramMap);


        return Result.ok();
    }


//    查询医院
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){

//        获取传递过来的医院信息
        Map<String,String[]>requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

//        获取医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2 根据传递过来医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
//        调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }


    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }



}
